package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbFeedback;
import com.ruoyi.system.domain.TbOrder;
import com.ruoyi.system.domain.vo.DriverIndexInfo;
import com.ruoyi.system.mapper.TbOrderMapper;
import com.ruoyi.system.service.ITbFeedbackService;
import com.ruoyi.system.service.ITbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-11
 */
@Service
public class TbOrderServiceImpl implements ITbOrderService {
    @Autowired
    private ITbFeedbackService feedbackService;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private ITbOrderService orderService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public TbOrder selectTbOrderById(Long id) {
        return tbOrderMapper.selectTbOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param tbOrder 订单
     * @return 订单
     */
    @Override
    public List<TbOrder> selectTbOrderList(TbOrder tbOrder) {
        return tbOrderMapper.selectTbOrderList(tbOrder);
    }

    /**
     * 新增订单
     *
     * @param tbOrder 订单
     * @return 结果
     */
    @Override
    public int insertTbOrder(TbOrder tbOrder) {
        tbOrder.setCreateTime(DateUtils.getNowDate());
        return tbOrderMapper.insertTbOrder(tbOrder);
    }

    /**
     * 修改订单
     *
     * @param tbOrder 订单
     * @return 结果
     */
    @Override
    public int updateTbOrder(TbOrder tbOrder) {
        tbOrder.setUpdateTime(DateUtils.getNowDate());
        return tbOrderMapper.updateTbOrder(tbOrder);
    }

    @Override
    public int updateTbOrderByOrderId(TbOrder tbOrder) {
        tbOrder.setUpdateTime(DateUtils.getNowDate());
        return tbOrderMapper.updateTbOrderByOrderId(tbOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByIds(Long[] ids) {
        return tbOrderMapper.deleteTbOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderById(Long id) {
        return tbOrderMapper.deleteTbOrderById(id);
    }

    @Override
    public DriverIndexInfo getIndexData(String currMerchantId) {
        DriverIndexInfo indexInfo = new DriverIndexInfo();
        //获取所有当日评价取平均量
        TbFeedback tbFeedback = new TbFeedback();
        tbFeedback.setMerchantId(currMerchantId);
        List<TbFeedback> feedbackList = feedbackService.selectTbFeedbackList(tbFeedback);
        if (CollUtil.isNotEmpty(feedbackList)) {
            BigDecimal avg = feedbackList.stream()
                    .map(TbFeedback::getVehicleCleanlinessRating) // 获取每个订单的金额
                    .reduce(BigDecimal.ZERO, BigDecimal::add);// 累加所有订单金
            indexInfo.setEvaluate(avg != null ? avg.divide(new BigDecimal(feedbackList.size()), 2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
        }
        indexInfo.setData(tbOrderMapper.obtainTheDataOfTheStatisticsPanel(currMerchantId));
        return indexInfo;
    }
}
